package com.cykj.service.impl;

import com.cykj.mapper.CardMapper;
import com.cykj.service.CardService;
import com.cykj.util.ResponseDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)//如果出现异常了直接回滚
    public ResponseDto addCardSingle(String prefix, int startNo, int endNo) {
        int count = 0;
        try {
            for (int i = startNo; i <= endNo ; i++) {
                String num = prefix + i;
                int res = cardMapper.addCardSingle(prefix,num);
                if (res >= 1){
                    count ++;
                }
                if (i == 3){
                    int j = 1/0;
                }
            }
            if (count == (endNo - startNo + 1 )){
                return new ResponseDto(1,"插入成功",null);
            }else {
                return new ResponseDto(1,"操作失败",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
